﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RoketPack.IM
{
    public partial class UserStatus : UserControl
    {
        private string p_Username = "";
        private string p_Status = "";
        private Contact.StateEnum p_State = Contact.StateEnum.OFFLINE;

        /// <summary>
        /// Creates a new UserStatus control.
        /// </summary>
        public UserStatus()
        {
            InitializeComponent();
        }

        /// <summary>
        /// This event is raised when the UserStatus control needs to
        /// (re)paint itself.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">The paint event information.</param>
        private void UserStatus_Paint(object sender, PaintEventArgs e)
        {
            Image statusimg = null;
            switch (this.p_State)
            {
                case Contact.StateEnum.ONLINE:
                    statusimg = Properties.Resources.IMOnline;
                    break;
                case Contact.StateEnum.BUSY:
                    statusimg = Properties.Resources.IMBusy;
                    break;
                case Contact.StateEnum.AWAY:
                    statusimg = Properties.Resources.IMAway;
                    break;
                case Contact.StateEnum.EXTENDEDAWAY:
                    statusimg = Properties.Resources.IMExtendedAway;
                    break;
                case Contact.StateEnum.INVISIBLE:
                    statusimg = Properties.Resources.IMInvisible;
                    break;
                case Contact.StateEnum.OFFLINE:
                    statusimg = Properties.Resources.IMOffline;
                    break;
                default:
                    statusimg = Properties.Resources.IMOffline;
                    break;
            }
            e.Graphics.DrawImage(statusimg, new Rectangle(8, 15, 16, 16), new Rectangle(0, 0, 16, 16), GraphicsUnit.Pixel);

            Font top = new Font(this.Font.FontFamily, 11.5f, GraphicsUnit.Pixel);
            Font bottom = new Font(this.Font.FontFamily, 10.0f, GraphicsUnit.Pixel);
            if (this.p_Status == null || this.p_Status.Trim() == "")
            {
                TextRenderer.DrawText(e.Graphics, this.p_Username, top, new Point(32, 16), Color.Black);
            }
            else
            {
                TextRenderer.DrawText(e.Graphics, this.p_Username, top, new Point(32, 6 + 4), Color.Black);
                TextRenderer.DrawText(e.Graphics, this.p_Status, bottom, new Point(32, 20 + 4), Color.DimGray);
            }
        }

        /// <summary>
        /// The username to be shown on the control.
        /// </summary>
        [Category("User Information")]
        public string Username
        {
            get
            {
                return this.p_Username;
            }
            set
            {
                this.p_Username = value;

                RoketPack.Manager.VoidLambda lambda = () =>
                    {
                        this.Invalidate(new Rectangle(32, 0, this.Width - 32, this.Height));
                    };
                if (this.InvokeRequired)
                    this.Invoke(lambda);
                else
                    lambda();
            }
        }

        /// <summary>
        /// The text status to be shown on the control.
        /// </summary>
        [Category("User Information")]
        public string Status
        {
            get
            {
                return this.p_Status;
            }
            set
            {
                this.p_Status = value;

                RoketPack.Manager.VoidLambda lambda = () =>
                {
                    this.Invalidate(new Rectangle(32, 0, this.Width - 32, this.Height));
                };
                if (this.InvokeRequired)
                    this.Invoke(lambda);
                else
                    lambda();
            }
        }

        /// <summary>
        /// The online state to be shown on the control.
        /// </summary>
        [Category("User Information")]
        public Contact.StateEnum State
        {
            get
            {
                return this.p_State;
            }
            set
            {
                this.p_State = value;

                RoketPack.Manager.VoidLambda lambda = () =>
                {
                    this.Invalidate(new Rectangle(0, 0, 32, this.Height));
                };
                if (this.InvokeRequired)
                    this.Invoke(lambda);
                else
                    lambda();
            }
        }
    }
}
